#include "TableView.h"
#include "ui_TableView.h"
#include <QLineEdit>
#include <QMessageBox>

TableView::TableView(QWidget *parent) :
	INoteView(parent),
	ui(new Ui::TableView)
{
	ui->setupUi(this);
	ui->tableWidget->setColumnCount(4);
	ui->tableWidget->setHorizontalHeaderLabels(QString(tr("Url;Login;Password;Comment")).split(';'));
	ui->tableWidget->verticalHeader()->setVisible(false);
	ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

	connect(ui->btnAddNote,SIGNAL(clicked()),SIGNAL(onBtnAddNote()));
	connect(ui->btnRemoveNote,SIGNAL(clicked()),SLOT(onBtnRemoveItem()));

	connect(ui->btnEdit,SIGNAL(clicked()),SLOT(onBtnEdit()));

	ui->btnAddNote->setStyleSheet(getButtonStyle("add"));
	ui->btnRemoveNote->setStyleSheet(getButtonStyle("delete"));
	ui->btnEdit->setStyleSheet(getButtonStyle("edit"));
}

TableView::~TableView()
{
	delete ui;
}

void TableView::onNoteAdded(Note* note)
{
	int row = ui->tableWidget->rowCount();
	ui->tableWidget->insertRow(row);

	QStringList list;
	list.append(note->getUrl());
	list.append(note->getLogin());
	list.append(note->getPass());
	list.append(note->getComment());

	for(int i=0;i<4;i++){
		QTableWidgetItem* item = new QTableWidgetItem(list.at(i));
		item->setData(Qt::UserRole, qVariantFromValue((void *) note));
		if(i==3) item->setToolTip(list.at(i));
		ui->tableWidget->setItem(row,i,item);
	}
	int column = ui->tableWidget->horizontalHeader()->sortIndicatorSection();
	Qt::SortOrder order = ui->tableWidget->horizontalHeader()->sortIndicatorOrder();
	ui->tableWidget->model()->sort(column, order);
}

void TableView::onNoteRemoved(Note* note)
{
	for(int i=0;i<ui->tableWidget->rowCount();i++){
		if(ui->tableWidget->item(i,0)->data(Qt::UserRole) == qVariantFromValue((void *) note)){
			ui->tableWidget->removeRow(i);
			return;
		}
	}
}

void TableView::onNoteUpdated(Note* note)
{
	for(int i=0;i<ui->tableWidget->rowCount();i++){
		if(ui->tableWidget->item(i,0)->data(Qt::UserRole) == qVariantFromValue((void *) note)){
			ui->tableWidget->item(i,0)->setText(note->getUrl());
			ui->tableWidget->item(i,1)->setText(note->getLogin());
			ui->tableWidget->item(i,2)->setText(note->getPass());
			ui->tableWidget->item(i,3)->setText(note->getComment());
			return;
		}
	}
	int column = ui->tableWidget->horizontalHeader()->sortIndicatorSection();
	Qt::SortOrder order = ui->tableWidget->horizontalHeader()->sortIndicatorOrder();
	ui->tableWidget->model()->sort(column, order);
}

void TableView::onBtnRemoveItem()
{
	QTableWidgetItem* item = ui->tableWidget->currentItem();
	if(item == NULL) return;
	Note* note = (Note*) item->data(Qt::UserRole).value<void*>();
	if(note == NULL) return;
	if(_provider){
		_provider->removeNote(note);
	}
}

void TableView::onBtnEdit()
{
	QTableWidgetItem* item = ui->tableWidget->currentItem();
	if(item == NULL) return;
	Note* note = (Note*) item->data(Qt::UserRole).value<void*>();
	if(note == NULL) return;
	emit onBtnEditNote(note);
}
